const post = getApp().post
const get = getApp().get

// 发送验证码
export const _senVCode = (mobile,cb)=>{
  let url =  '/member/send/vcode/?mobile='+mobile
  let data = {}
  get(url,data,cb)
}

// 获取单个diary
export const _getDiary=(diary_id,cb)=>{
  let url = '/member/diary/'+diary_id+'/detail/'
  let data = {
    // limit: pageSize,
    // page: pageNum
  }
  get(url,data,cb)
}
// 获取media列表
export const _getMediaList=(type,cb)=>{
  let url = '/member/media/hot/?type='+type
  let data = {
    // limit: pageSize,
    // page: pageNum
  }
  get(url,data,cb)
}
// 获取单个media
export const _getMediaDetail=(media_id,cb)=>{
  let url = '/member/media/'+media_id+'/detail/'
  let data = {
    // limit: pageSize,
    // page: pageNum
  }
  get(url,data,cb)
}
// 评论
export const _sendComment=(data,cb)=>{
  let url = '/member/comment/'
  post(url,data,cb)
}
//用户点赞
export const _memberLike=(object_id,model_name,cb)=>{
  let url = '/member/like/?object_id='+object_id+'&model_name='+model_name
  let data = {}
  get(url,data,cb)
}
//用户收藏
export const _memberCollect=(object_id,model_name,cb)=>{
  let url = '/member/collect/?object_id='+object_id+'&model_name='+model_name
  let data = {}
  get(url,data,cb)
}
export const _isEmptyObject = (obj)=>{
  for (var n in obj) {
      return false
  }
  return true; 
} 
// 获取diary列表
export const _getNextPageDiaryList=(cb,page_data={},diary_data=[])=>{
  let pageNum = 1
  let flag = true
  if(!_isEmptyObject(page_data)){
    let {totalPage,currentPage} = page_data
    pageNum = currentPage + 1
    if(pageNum > totalPage){
      flag = false
    }
  }
  if(flag){
    let url = '/member/diary/hot/'
    let data={}
    get(url,data,res=>{
      console.log(res)
      if(res.statusCode == 200 && res.data.status){
        let {totalPage,currentPage,dataList} = res.data.data
        page_data['totalPage'] = totalPage
        page_data['currentPage'] = currentPage
        diary_data = diary_data.concat(dataList)
        return cb({'diary_data':diary_data,'page_data':page_data})
      }
    },{'currentPage':pageNum})
    return cb({})
  }
}

//获得评论
export const _getNextPageComments=(object_id,model_name,cb,comment_page_data={},comment_data=[])=>{
  let pageNum = 1
  let flag = true
  if(!_isEmptyObject(comment_page_data)){
    let {totalPage,currentPage} = comment_page_data
    let nextPage = currentPage + 1
    if(nextPage > totalPage){
      flag = false
    }
    pageNum = nextPage
  }
  if(flag){
    let url = '/member/comment/comments/?object_id='+object_id+'&model_name='+model_name
    let data={}
    get(url,data,res=>{
      let {totalPage,currentPage,dataList} = res.data.data
      comment_page_data['totalPage'] = totalPage
      comment_page_data['currentPage'] = currentPage
      if(!_isEmptyObject(comment_data)){
        comment_data = comment_data.concat(dataList)
      }else{
        comment_data = dataList
      }
      return cb({'comment_data':comment_data,'comment_page_data':comment_page_data})
    },{'currentPage':pageNum})
    return cb({})
  }
    
}
// 点赞、收藏操作
export const _likeCollectOperate=(operate,instance)=>{
  switch (operate) {
    case 'like':
      if(instance.is_like){
        instance.like_count --
      }else{
        instance.like_count ++
      }
      instance.is_like = !instance.is_like
      break;
    case 'collect':
      instance.is_collect = !instance.is_collect
      break;
    default:
      console.log('-----default---');
  }
  return instance  
}
